import { post } from '@/utils/request'
import { Reducer } from 'redux'

export interface IDefaultAuthInfo {
  token: string
  tokenTime: number
}
const defaultvalue: IDefaultAuthInfo = {
  token: '',
  tokenTime: 0,
}

export function postLogin(account: any, password: any) {
  return {
    type: 'login',
    payload: post('/api/login', { account, password }, { needToken: false }),
  }
}

export const authInfo: Reducer<IDefaultAuthInfo> = (
  // 第一个代表的是state，默认类型为泛型参数一的类型
  // 第二个代表的是action，默认类型参数二的类型
  state = defaultvalue,
  { type, payload }
) => {
  switch (type) {
    case 'login':
      return {
        ...state,
        ...payload.data,
      }
    default:
      return state
  }
}
export default authInfo
